<?php

/*
 * Class designed to manage match planning
 */

class Link {

  /*
   * Unique id of the link
   */

  public $id = null;

  /*
   * Content of the link
   */

  public $title = null;
  
  public $url = null;


  public function __construct( $data=array() ) {
    if ( isset( $data['id'] ) ) $this->id = (int)$data['id'];
    if ( isset( $data['title'] ) ) $this->title = $data['title'];
    if ( isset( $data['url'] ) ) $this->url = $data['url'];
  }


  public function storeFormValues( $params ) {
	  $this->__construct( $params );
  }


  public static function getById( $id ) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM link WHERE id = :id";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $id, PDO::PARAM_INT );
    $st->execute();
    $row = $st->fetch();
    $conn = null;
    if ( $row ) return new Link( $row );
  }

  public static function getList( $numRows=1000000) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM link
            LIMIT :numRows";

    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->execute();
    $list = array();

    while ( $row = $st->fetch() ) {
      $link = new Link( $row );
      $list[] = $link;
    }

    // Now get the total number of Match that matched the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";
    $totalRows = $conn->query( $sql )->fetch();
    $conn = null;
    return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
  }


  public function insert() {

    // Does the Match object already have an ID?
    if ( !is_null( $this->id ) ) trigger_error ( "Link::insert(): Attempt to insert an Link object that already has its ID property set (to $this->id).", E_USER_ERROR );

    // Insert the Match
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $this->id = $conn->lastInsertId();
    $sql = "INSERT INTO link ( id, title, url ) VALUES ( :id, :title, :url )";
    $st = $conn->prepare ( $sql );
    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $st->bindValue( ":title", $this->title, PDO::PARAM_STR );
    $st->bindValue( ":url", $this->url, PDO::PARAM_STR );
    $st->execute();
    $conn = null;
  }


  public function update() {
      if ( is_null( $this->id ) ) trigger_error ( "Link::update(): Attempt to update an Link object that does not have its ID property set.", E_USER_ERROR );

      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "UPDATE link SET title=:title, url=:url WHERE id = :id";
      $st = $conn->prepare ( $sql );
      $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
      $st->bindValue( ":title", $this->title, PDO::PARAM_STR );
      $st->bindValue( ":url", $this->url, PDO::PARAM_STR );
      $st->execute();
      $conn = null;
    }


  public function delete() {
    if ( is_null( $this->id ) ) trigger_error ( "Link::delete(): Attempt to delete an Link object that does not have an ID property.", E_USER_ERROR );

    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "DELETE FROM link WHERE id=:id LIMIT 1";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
  }

}
?>
